﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TSV.Models;
using TSV.Library.Common;
using TSV.Library.Attributes;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
using TSV.Areas.Administration.Models;
using System.Text;
using Pagination;
using Excel = Microsoft.Office.Interop.Excel;
namespace TSV.Controllers
{
    public class HomeController : BaseViewController
    {
        #region Variable
        AdvertisingDao advertisingDao = new AdvertisingDao();
        int defaultPageSize = Constants.DEFAULT_PAGE_SIZE;
        #endregion
        public ActionResult Index(int? page)
        {
            
            ViewData["CategoryList"] = new CategoryDao().GetList();
            List<Partner> partnerList = new PartnerDao().GetAllPartner();
            ViewData["PartnerList"] = partnerList;
            int currentPageIndex = page.HasValue ? page.Value : 1;
            var list = partnerList.ToPagedList(currentPageIndex, defaultPageSize);
            if (Request.IsAjaxRequest())
                return PartialView("ProductList", list);
            else
                return View(list);
        }

        [HttpPost]
        public JsonResult CreateEmail(string email)
        {
            JsonResult result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            EmailRegisterDao dao = new EmailRegisterDao();
            Message msg = null;
            if (CheckUtil.IsEmail(email))
            {
                if (dao.CheckEmailExits(email) == null)
                {
                    Email objEmail = new Email();
                    objEmail.Email1 = email;
                    msg = dao.Insert(objEmail);
                    if (msg.MsgType == MessageType.Info)
                    {
                        msg = new Message(MessageConstants.I0005, MessageType.Info);
                    }
                }
                else
                {
                    msg = new Message(MessageConstants.E0041, MessageType.Error);
                   
                }
            }
            else
            {
                msg = new Message(MessageConstants.E0004, MessageType.Error);
            }
            result.Data = msg;
            return result;
        }

        public ActionResult SetFilter(FormCollection request)
        {
            Hashtable hash = new Hashtable();
            hash.Add(Constants.GLOBAL_FILTER_TEXT, request["txtGlobalSearch"] != Constants.GLOBAL_FILTER_TEXT_DEFAULT?request["txtGlobalSearch"]:string.Empty);
            hash.Add(Constants.GLOBAL_FILTER_CATEGORY, request["GlobalCategory"]);
            Session[SessionKey.GLOBALSEARCHDATA] = hash;
            return Redirect("/timkiem.html");
        }

        public ActionResult GlobalSearch(int? page)
        {
            ViewData["AdvertisingLeft"] = new AdvertisingDao().GetAdvertisingLeft();
            string text = string.Empty;
            int category = 0;
            if (Session[SessionKey.GLOBALSEARCHDATA] != null)
            {
                Hashtable hash = (Hashtable)Session[SessionKey.GLOBALSEARCHDATA];
                text = (string)hash[Constants.GLOBAL_FILTER_TEXT];
                ViewData["NavigationLink"] =text;
                string value = (string)hash[Constants.GLOBAL_FILTER_CATEGORY];
                if (!string.IsNullOrEmpty(value) && CheckUtil.IsPositiveNumber(value))
                {
                    category = int.Parse(value);
                }
            }
            List<sp_GetGlobalSearchResult> partnerList = new PartnerDao().GetListByGlobalSearch(text, category);
            int currentPageIndex = page.HasValue ? page.Value : 1;
            var list = partnerList.ToPagedList(currentPageIndex, defaultPageSize);
            if (Request.IsAjaxRequest())
                return PartialView("List", list);
            else
                return View(list);
        }
        public void ReadInterviewDataFromExcel()
        {
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(Server.MapPath(Constants.UPLOAD_ADV_ + "CAM.xls"));
            Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets.get_Item(1);
             Excel.Range range;
             List<Partner> list = new List<Partner>();
            try
            {
                for (int i = 4; i <= 43; i++)
                {
                    Partner partner = new Partner();
                    range = xlWorkSheet.get_Range("G" + i);
                    partner.Logo = "cam"+range.Text+".png";
                    range = xlWorkSheet.get_Range("C" + i);
                    partner.PartnerName = range.Text;
                    range = xlWorkSheet.get_Range("F" + i);
                    partner.CategoryID = int.Parse(range.Text);
                   // partner.CategoryID =29;
                    //partner.Webiste = collection["Webiste"];
                    //partner.Email = collection["Email"];
                   // partner.Description = collection["Description"];
                    //partner.DiscountInfo = collection["DiscountInfo"];
                    partner.PartnerOrder = 34;
                    partner.DeleteFlag = false;
                    partner.IsNew = true;
                    partner.IsActive = true;
                    partner.Manufacturer = "Cam";
                    range = xlWorkSheet.get_Range("B" + i);
                    partner.CodeID = range.Text;
                     range = xlWorkSheet.get_Range("E" + i);
                     partner.CurrentPrice = double.Parse(range.Text);
                    partner.ExpireDate = DateTime.Now;
                    partner.IsAvailable =true;
                    partner.Status = "Hàng mới về";
                    partner.ReceivedTime ="2 ngày";
                    partner.CreatedBy ="phihung.nguyen";
                    partner.CreateDate = DateTime.Now;
                    partner.UpdatedBy = "phihung.nguyen";
                    partner.UpdateDate = DateTime.Now;
                    list.Add(partner);
                }
            }
            finally
            {
                xlWorkBook.Close();
                xlApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                xlWorkSheet = null;
                xlWorkBook = null;
                xlApp = null;
            }
           new PartnerDao().InsertMulti(list);
            //return list;
        }

        public void UpdateCodeFromExcel()
        {
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(Server.MapPath(Constants.UPLOAD_ADV_ + "NDC.xls"));
            Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets.get_Item(1);
            Excel.Range range;
            try
            {
                for (int i = 4; i <= 28; i++)
                {
                    range = xlWorkSheet.get_Range("B" + i);
                    Partner obj = new PartnerDao().GetPartnerByCodeID(range.Text);
                    if (obj != null)
                    {
                       // range = xlWorkSheet.get_Range("F" + i);
                        obj.Manufacturer = "VN";
                        new PartnerDao().UpdateCodeID(obj);
                    }
                }
            }
            finally
            {
                xlWorkBook.Close();
                xlApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                xlWorkSheet = null;
                xlWorkBook = null;
                xlApp = null;
            }
            //return list;
        }
    }
}
